﻿using AutoMapper;
using Model;
using Repository;
using Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Base
{
    public class BaseService<Tentity, TVo> : IBaseService<Tentity, TVo> where Tentity : class, new()
    {

        private readonly IMapper _mapper;
        private readonly IBaseRepository<Tentity> _baseRepository;


        public BaseService(IMapper mapper ,IBaseRepository<Tentity> baseRepository)
        {
            _mapper = mapper;
            _baseRepository = baseRepository;
        }

        public async Task<int> addUserAsync(TVo vo)
        {
            return await _baseRepository.AddAsync(_mapper.Map<Tentity>(vo));
        }

        public Task<int> deleteUserAsync(TVo vo)
        {
            return _baseRepository.DeleteAsync(_mapper.Map<Tentity>(vo));
        }

        public async Task<List<TVo>> GetAllUserAsync()
        {
            var dataList = await _baseRepository.GetAllAsync();
            return _mapper.Map<List<TVo>>(dataList);
        }

        public async Task<TVo> GetUserByIdAsync(int id)
        {
            var data = await _baseRepository.GetByIdAsync(id);
            return _mapper.Map<TVo>(data);
        }

        public async Task<List<TVo>> Query()
        {
            var data = await _baseRepository.Query();
            return _mapper.Map<List<TVo>>(data);
        }

        public Task<int> updateUserAsync(TVo vo)
        {
            return _baseRepository.UpdateAsync(_mapper.Map<Tentity>(vo));
        }
    }
}
